package cn.edu.zjgsu.ito.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.zjgsu.ito.model.ComplainView;
import cn.edu.zjgsu.ito.model.CourierVo;
import cn.edu.zjgsu.ito.service.IAdminService;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.Page;

@Controller
@RequestMapping(value = "/AdminController")
@Transactional
public class AdminUserController
{

	@Autowired
	private IAdminService adminService;

	/**
	 * 扣除用户信誉分
	 * 
	 * @param userId
	 * @param score
	 * @return
	 */
	@RequestMapping(value = "deductUserIntegralScore/{userId}/{integralScore}", method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String, Object> deductScore(@PathVariable int userId,
			@PathVariable int integralScore)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		if (integralScore <= 0)
		{
			return error(result);
		}
		adminService.deductUserIntegralScore(userId, integralScore);
		return success(result);
	}
	
	
	
	/**
	 * 快递员审核列表
	 * 
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/couriers/approval", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> getCouriers(@RequestParam(name="sequence",required=false)String sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();		
		int sequenceInt = 0;
		if(sequence==null)
			sequenceInt = 0;
		else
			sequenceInt = Integer.valueOf(sequence);
		
		List<CourierVo> courierVos = adminService.getCouriersApproval(sequenceInt);
		result.put("couriers", courierVos);
		return success(result);
	}

	
	
	

	/**
	 * 快递员审核通过
	 * 
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/courier/pass/{courierId}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> passCourierByCourierId(
			@PathVariable int courierId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeCourierState(courierId, 3);
		return success(result);
	}

	/**
	 * 快递员审核批量通过
	 * 
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/couriers/pass", method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public Map<String, Object> passCouriersByCourierId(@RequestBody Map map)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<Integer> courierIds = (List<Integer>) map.get("courierIdS");
		for (int courierId : courierIds)
		{
			adminService.changeCourierState(courierId, 3);
		}
		return success(result);
	}

	/**
	 * 快递员审核驳回
	 * 
	 * @param courierId
	 * @return
	 */
	@RequestMapping(value = "/courier/notPass/{courierId}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> notPassCourierByCourierId(
			@PathVariable int courierId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.changeCourierState(courierId, 4);
		return success(result);
	}

	/**
	 * 分页查看投诉情况
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @return
	 *//*
	@RequestMapping(value = "/complain/pageCount/{state}/{pageCount}/pageSize/{pageSize}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> getComplainByPaging(@PathVariable int state,
			@PathVariable int pageCount, @PathVariable int pageSize)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		Page<ComplainView> page = adminService.getComplainByPaging(state,
				pageCount, pageSize);
		result.put("complains", page.getData());
		result.put("count", page.getTotalCount());
		return success(result);
	}*/
	
	
	
	
	/**
	 * 分页查看投诉情况
	 * 
	 * @param pageCount
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/complain", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> getComplain(@RequestParam(name="sequence",required=false) String sequence)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		List<ComplainView> ComplainViews = adminService.getComplainByPaging(sequence==null?0:Integer.valueOf(sequence));
		result.put("complains", ComplainViews);
		return success(result);
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 根据投诉单id获取投诉详情
	 * @param complainId
	 * @return
	 */
	@RequestMapping(value = "/complain/{complainId}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> getComplainById(@PathVariable int complainId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		ComplainView complain = adminService.getComplainById(complainId);
		result.put("complain", complain);
		return success(result);
	}
	
	
	
	
	
	
	
	/**
	 * 完成投诉
	 * @param complainId
	 * @return
	 */
	@RequestMapping(value = "/complain/complete/{complainId}", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public Map<String, Object> getComplainByPaging(@PathVariable int complainId)
	{
		Map<String, Object> result = new HashMap<String, Object>();
		adminService.completeComplain(complainId);
		return success(result);
	}
	
	
	
	
	
	
	

	private Map<String, Object> error(Map<String, Object> result)
	{
		result.put("code", Constant.FAIL);
		return result;
	}

	private Map<String, Object> success(Map<String, Object> result)
	{
		result.put("code", Constant.OK);
		return result;
	}

}
